<template>
  <div class="collection-container">
    <!-- 头部导航 -->
    <div class="header">
      <van-nav-bar title="我的收藏" left-text="返回" right-text="商城" @click-right="$router.push('/mall')" left-arrow @click-left="$router.back()" />
    </div>
    <van-empty description="暂无收藏数据，去商城逛逛吧！" v-if="goodsList.length === 0" />

    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" v-else>
      <van-list offset="0" :immediate-check="false" v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <!-- 其他商品 -->
        <div class="another-container">
          <div class="another-list">
            <div class="another-item" v-for="an in goodsList" :key="an.id" @click="toDetail(an.id)">
              <div class="goods-img">
                <van-image lazy-load :src="'http://www.dsl2023hcy.love:3004' + an.goods_img"></van-image>
              </div>
              <!-- 商品信息 -->
              <div class="goods-content">
                <div class="goods-title">{{ an.goods_name }}</div>
                <div class="price-content">
                  <div class="price">
                    ￥<span class="number">{{ dealPrice(an.goods_price)[0] }}</span>
                    <span>.{{ dealPrice(an.goods_price)[1] }}</span>
                    元
                  </div>
                  <div class="delete-icon" @click.stop="cancelLike(an.collection_id)">
                    <van-icon name="delete" />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import axios from '@/utils/request/request'
import { Notify } from 'vant'
export default {
  name: 'collection',
  data() {
    return {
      page: 1,
      limit: 10,
      goodsList: [],
      isLoading: false,
      loading: false,
      finished: false,
      canLoading: false
    }
  },
  methods: {
    // 前往商品详情页
    toDetail(id) {
      this.$router.push('/goods/detail/' + id)
    },
    // 处理价格
    dealPrice(price) {
      const priceArr = price.toFixed(2).split('.')
      return priceArr
    },
    // 获取所有商品数据
    async getAllCollection() {
      this.$toast.loading({
        message: '加载中...',
        duration: 0
      })
      try {
        const data = await axios.get('/user/all/collection', {
          params: {
            page: this.page,
            limit: this.limit
          }
        })
        if (data.code === 0) {
          this.loading = false
          this.goodsList = [...this.goodsList, ...data.data]
          if (data.data.length < this.limit) {
            this.finished = true
          }
        } else if (data.code === 403 || data.msg === '身份认证错误') {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000,
            onClose: () => {
              localStorage.removeItem('user_token')
              this.$router.replace('/login')
            }
          })
        } else {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000
          })
        }
      } catch {
        Notify({
          type: 'danger',
          message: '请求失败！',
          duration: 1000
        })
      } finally {
        this.$toast.clear()
      }
    },
    // 上拉加载
    onLoad() {
      this.page++
      this.getAllCollection()
    },
    // 下拉刷新
    async onRefresh() {
      this.finished = false
      this.page = 1
      this.goodsList = []
      this.getAllCollection()
      this.isLoading = false
    },
    async cancelLike(id) {
      this.$toast.loading({
        message: '加载中...',
        duration: 0
      })
      try {
        // 取消收藏
        const data = await axios.post('/user/cancel/like', {
          id
        })
        if (data.code === 0) {
          Notify({
            type: 'success',
            message: data.msg,
            duration: 1000
          })
          this.goodsList = this.goodsList.filter((item) => item.collection_id !== id)
        } else if (data.code === 403 || data.msg === '身份认证错误') {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000,
            onClose: () => {
              localStorage.removeItem('user_token')
              this.$router.replace('/login')
            }
          })
        } else {
          Notify({
            type: 'danger',
            message: data.msg,
            duration: 1000
          })
        }
      } catch {
        Notify({
          type: 'danger',
          message: '请求失败！',
          duration: 1000
        })
      } finally {
        this.$toast.clear()
      }
    }
  },
  created() {
    this.getAllCollection()
  }
}
</script>

<style lang="less" scoped>
.collection-container {
  padding-top: 46px;
  min-height: 100vh;
  background-color: #f5f5f5;
  ::v-deep .van-empty {
    padding-top: 80px;
  }
  .header {
    position: fixed;
    top: 0;
    left: 0;
    height: 46px;
    border-bottom: 1px solid #f1f1f1;
    width: 100vw;
    z-index: 999;

    ::v-deep .van-nav-bar .van-icon,
    ::v-deep .van-nav-bar__text {
      color: #323232;
    }
  }

  .another-container {
    background-color: #f1f1f1;
    padding: 0 10px;
    .another-list {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;

      .another-item {
        background-color: #fff;
        width: 48%;
        padding: 5px 5px 20px;
        margin-bottom: 10px;
        border-bottom-left-radius: 10px;
        border-bottom-right-radius: 10px;

        .goods-img {
          display: flex;
          align-items: center;
          justify-content: center;
          min-height: 100px;
          min-width: 100px;
        }

        ::v-deep .van-image {
          width: 100%;
          height: auto;
        }

        img {
          width: 100%;
          height: auto;
        }

        .goods-content {
          background-color: #fff;
          padding: 0 10px;

          .goods-title {
            margin-top: 10px;
            font-size: 14px;
            font-weight: 600;
            overflow: hidden;
            text-overflow: ellipsis; /*显示为......*/
            -webkit-line-clamp: 2; /*显示为两行，在第二行中显示为.......*/
            display: -webkit-box;
            -webkit-box-orient: vertical;
          }

          .price-content {
            display: flex;
            align-items: center;
            justify-content: space-between;
            .price {
              margin-top: 5px;
              font-size: 12px;
              color: #ee0a24;
              padding-bottom: 10px;
              span {
                font-weight: 700;
              }

              .number {
                font-size: 16px;
              }
            }

            .delete-icon {
              ::v-deep .van-icon {
                font-size: 14px;
                color: #ff9018;
              }
            }
          }
        }
      }
    }
  }
}
</style>
